#ifndef COMMROBOTIQ_H
#define COMMROBOTIQ_H

#include <QObject>

#define D_REG_MAX 10
#define D_USE_QT_SERIAL 0

#include <QTimer>
#include <QMutex>
#include "gSoapFoundation/soapStub.h"


#if D_USE_QT_SERIAL
#include <QSerialPort>
#include <QSerialPortInfo>
#else
#include "modbus.h"
#include "modbus-rtu.h"
#endif


enum E_ROBOTIQ_STATUS{
    eADF,
};

/**
 * @brief The CommRobotiq class
 * 2017-7-21-xyf
 * 2017-8-3-xyf, 添加锁保护
 */
class CommRobotiq : public QObject
{
    Q_OBJECT
public:
    explicit CommRobotiq(QObject *parent = 0);

    int readVersion(std::vector <ComponentVersion> &componentVersionList);

    quint16 r_reg[D_REG_MAX];
    quint16 w_reg[D_REG_MAX];

signals:
    void signal_error( short errorCode );
    void signal_message( QString str );
    void signal_found_obj( short pos );
    void signal_not_found(  );

public slots:
    void checkObj();
    int connectRobotiq( );
    int disconnectRobotiq( );
    int acitved();
    int readStatus( int& state );
    void clearError();

    void setPos( short pos );
    void setSpeed( short sp );
    void setTorque( short tq );
 //   void quickStop();
    void stop();
    void move(short pos, short sp, short tq );

    void checkStatus( );//定时检测
    void message( QString str ){
        emit signal_message( str );
    }

private slots:

private:

#if D_USE_QT_SERIAL
    QSerialPortInfo m_serialport_info;
    QSerialPort  m_serialport;
#else
    modbus_t *mb;
#endif
    QTimer timer;
    bool isCheckObj;
    QMutex qtlock;

    ComponentVersion versionInfo;

};

#endif // COMMROBOTIQ_H
